﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using NetCoreCMS.Common;
using NetCoreCMS.IServices;
using NetCoreCMS.Models;

namespace NetCoreCMS.Web.Areas.WebApi.Controllers
{
    public class Shop_Point_TixianController : UserController
    {
        public IShopService _shop { get; set; }
        public IShop_ApplyService _shop_apply { get; set; }
        protected Userconfig userConfig;
        [Authorize]
        [HttpGet]
        public IActionResult Get(decimal point)
        {
            userConfig = Common.XMLConfig.Userconfig.loadConfig(_env.ContentRootPath);
            //检查用户是否登录
            var model = GetUserInfo();
            var shop = _shop.GetFirstByUserId(model.id);
            if (model == null)
            {
                return Ok(new { status = 0, msg = "温馨提示：用户尚未登录或已超时！" });
            }
            point = Math.Floor(point / userConfig.tixian_base) * userConfig.tixian_base;
            if (userConfig.tixian_switch == 1)
            {
                return Ok(new { status = 0, msg = "温馨提示：提现功能关闭！" });
            }
            //提现
            if (shop.point < point)//现金积分
            {
                return Ok(new { status = 0, msg = "温馨提示：金额不足！" });
            }
            //生成申请记录
            Shop_Apply ap = new Shop_Apply();
            ap.shop_id = shop.id;
            ap.apply_point = point;
            bool flag = _shop_apply.Add(ap);
            if (!flag)
                return Ok(new { status = 0, msg = "温馨提示：提现失败！" });
            //保存成功后返回
            return Ok(new { status = 1, msg = "提现申请成功！" });
        }
    }
}